package com.chaoma.web.controller.cm;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chaoma.common.core.controller.BaseController;
import com.chaoma.common.core.domain.AjaxResult;
import com.chaoma.system.domain.cmUser;
import com.chaoma.system.service.IcmUserService;
import com.chaoma.common.utils.poi.ExcelUtil;
import com.chaoma.common.core.page.TableDataInfo;

/**
 * 用户Controller
 *
 * @author chaoma
 * @date 2023-10-07
 */
@RestController
@RequestMapping("/system/cmUser")
public class cmUserController extends BaseController {
    @Autowired
    private IcmUserService cmUserService;

    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(cmUser cmUser) {
        startPage();
        List<cmUser> list = cmUserService.selectcmUserList(cmUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, cmUser cmUser) {
        List<cmUser> list = cmUserService.selectcmUserList(cmUser);
        ExcelUtil<cmUser> util = new ExcelUtil<cmUser>(cmUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cmUserService.selectcmUserById(id));
    }

    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@RequestBody cmUser cmUser) {
        cmUser cmUser1 = new cmUser();
        cmUser1.setImei(cmUser.getImei());
        List<cmUser> cmUsers = cmUserService.selectcmUserList(cmUser1);
        if (cmUsers != null && cmUsers.size() > 0) {
            return AjaxResult.error("设备号已存在");
        }
        return toAjax(cmUserService.insertcmUser(cmUser));
    }

    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult edit(@RequestBody cmUser cmUser) {
        Long id = cmUser.getId();
        cmUser cmUser1 = new cmUser();
        cmUser1.setImei(cmUser.getImei());
        List<cmUser> cmUsers = cmUserService.selectcmUserList(cmUser1);
        if (cmUsers != null && cmUsers.size() > 0) {
            for (cmUser user : cmUsers) {
                if (user.getId() != id) {
                    return AjaxResult.error("设备号已存在");
                }
            }
        }
        return toAjax(cmUserService.updatecmUser(cmUser));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmUserService.deletecmUserByIds(ids));
    }
}
